package cn.qmso.wxPay.v3.config;

import cn.qmso.wxPay.v3.base.Pay;
import cn.qmso.wxPay.v3.base.WxPayV3Content;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.security.cert.CertificateException;

@EnableScheduling
@Slf4j
public class AutoRefreshWxCertificate {

    @Autowired
    private WxPayV3Config wxPayV3Config;

    /**
     * 自动更新证书 12小时更新一次
     */
    @Scheduled(fixedRate = 1000 * 60 * 60 * 12)
    public void refreshCertificate() throws CertificateException, NoSuchAlgorithmException, IOException, SignatureException, InvalidKeyException {
        log.info("开始自动更新微信平台证书===============>");
        Pay.refreshCertificate(WxPayV3Content.URL_PRE + WxPayV3Content.V3_CERTIFICATES_URL, wxPayV3Config);
        log.info("微信平台证书更新完成<==================");
    }
}
